﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.IO;
using Microsoft.Win32;
using System.Data.SqlClient;

namespace SampleWPFApplciation
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {       
        public MainWindow()
        {
            InitializeComponent();
            
            ObservableCollection<EmployeeDetails> details = new ObservableCollection<EmployeeDetails>();
            //List<EmployeeDetails> details = new List<EmployeeDetails>();

            //details.Add(new EmployeeDetails(){ID=1,Name="Ashok",Address="Pondy"
            //,Contact=12345,Email="santaashok@gmail.com",Status="Active"});
            //details.Add(new EmployeeDetails() { ID = 2, Name = "kumar",Address="bangalore"
            //,Contact = 06789,Email = "ashokkumar@majckltrading.com",Status = "InActive"
            //});

            string connectionstring =@"Data Source=ashokkumar-pc\sqlexpress;Initial Catalog=EmployeeDB;Integrated Security=true;";
            SqlConnection con = new SqlConnection(connectionstring);
            SqlCommand cmd = new SqlCommand("select * from Emp_Details", con);
            con.Open();         
            SqlDataReader reader = cmd.ExecuteReader();
               
            while (reader.Read())
            {
                details.Add(new EmployeeDetails()
                {
                    ID = Convert.ToInt32(reader.GetValue(0)),
                    Name = reader.GetValue(1).ToString(),
                    Address = reader.GetValue(2).ToString(),
                    Contact = Convert.ToDouble(reader.GetValue(3)),
                    Email = reader.GetValue(4).ToString(),
                    Status = reader.GetValue(5).ToString()
                });
            }
            reader.Close();
            con.Close();
            dataGrid1.ItemsSource = details;   
         
            //this.DataContext = new EmployeeDetails() { ID = 2, Name = "kumar",Address="bangalore"
            //,Contact = 06789,Email = "ashokkumar@majckltrading.com",Status = "InActive"
            //};
        }

        private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {            
            if (dataGrid1.Items.Count > 0)
            {
                dataGrid2.ItemsSource = dataGrid1.SelectedItems;
                this.DataContext = dataGrid1.SelectedItems;
            }

        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {            
            MessageBoxResult result =  MessageBox.Show("Do you want to exit", "Exit", MessageBoxButton.YesNo, MessageBoxImage.Question);

            if (result == MessageBoxResult.Yes)
            {
                this.Close();
            }
            else
            {                
                MessageBox.Show("Exit Operation Cancelled", "Message");
            }

        }

        private void btnFolderCreate_Click(object sender, RoutedEventArgs e)
        {            
            string folderName = "C:\\Sample";            
            if (Directory.Exists(folderName))
            {
                string[]files =  Directory.GetFiles(folderName);
                foreach (var item in files)
                {
                    File.Delete(item);                    
                }
                Directory.Delete(folderName);
                MessageBox.Show("Directory Deleted");
            }           
            Directory.CreateDirectory(folderName);
        }

        private void btnCreateFile_Click(object sender, RoutedEventArgs e)
        {
            
            string fileName = "C:\\Sample\\sample.txt";
            StreamWriter writter = new StreamWriter(fileName);
            
            writter.WriteLine("text1");
            writter.WriteLine("text2");
            writter.Write("text3");
            writter.Close();
            
        }

        private void btnBrowseFile_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            
            openDialog.ShowDialog();
            string selectedFileName = openDialog.FileName;
            
            StreamReader reader = new StreamReader(selectedFileName);
            string line = null;
            while ((line = reader.ReadLine()) != null)
            {
                MessageBox.Show(line);                
            }

        }   
    }
}
